package com.deity.news.service;

import java.util.ArrayList;
//import java.util.Iterator;
import java.util.List;

import com.deity.news.service.BaseService;
import com.deity.news.bean.Document;
import com.deity.news.bean.Category;
import com.deity.news.service.CategoryService;
import com.deity.news.dao.DBConnect;
import com.deity.news.dao.TempCategoryDAO;
import com.deity.news.dao.TempDocumentDAO;

public class DocumentService {

	public static int amount(int cid) {
		// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();

		return TempDocumentDAO.amount(cid);
	}

	public static List find(int cid, int start, int size) {
		// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();
		List<Document> list1 = new ArrayList<Document>();

		list1 = TempDocumentDAO.find(cid, start, size);
		if (list1.size() > 0) {
			Category category = CategoryService.findById(cid);
			for (int i = 0; i < list1.size(); i++) {
				Document doc = list1.get(i);
				doc.setCategory(category);
			
			}
		}
		return list1;

	}

	// public static Document fill(int did){
	// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();
	// Document doc=documentDao.findById(did);
	// Category c=CategoryService.findById(doc.getCid());
	// doc.setCategory(c);
	// return doc;
	// }

	public static int removeDocument(Document document) {
		BaseService baseService = new BaseService();
		DBConnect dbc = null;
		// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();
		
		
		int rresult1 = 0;
		int rresult2 = 0;
		int rresult3 = 0;
		try {
			dbc = baseService.createTransaction(0);
			rresult1 = TempDocumentDAO.removeDocument(dbc, document);
			Category c = CategoryService.findByIdFU(dbc, document.getCid());
			c.setAmount(c.getAmount() - 1);
			rresult2 = CategoryService.updateCategoryFU(dbc, c);
			if (rresult1 == 1 && rresult2 == 1) {
				baseService.commitTransaction(dbc);
				rresult3 = 1;
			} else
				baseService.rollbackTransaction(dbc);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		return rresult3;
	}

	public static int returnNextseq() {
		// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();

		return TempDocumentDAO.returnNextseq();
	}

	/*
	 * public List findAll(int start, int size){ DocumentDAO documentDao =
	 * DocumentDAO.getInstance(); return documentDao.findAll(start,size); }
	 */
	public static Document findById(int did) {
		// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();
		Document doc = TempDocumentDAO.findById(did);
		Category c = CategoryService.findById(doc.getCid());
		doc.setCategory(c);
		return doc;

	}

	public static List findByCid(int cid) {
		// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();
		List<Document> l = new ArrayList<Document>();
		Category category = CategoryService.findById(cid);
		l = TempDocumentDAO.findByCid(cid);
		if (l.size() > 0) {
		for (int i = 0; i < l.size(); i++) {
			Document d = (Document)l.get(i);
			d.setCategory(category);
		
		}
	    }
		return l;
	}
	
	public static List convertToList(List list) {
		// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();
		return TempDocumentDAO.convertToList(list);
	}

	public static int addDocument(Category category, Document document) {
		// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();
		BaseService baseService = new BaseService();
		DBConnect dbc = null;

		Category c = null;

		int rresult1 = 0;
		int rresult2 = 0;
		int rresult3 = 0;
		try {
			dbc = baseService.createTransaction(0);
			rresult1 = TempDocumentDAO.addDocumentFU(dbc, document);
			category.setAmount(category.getAmount() + 1);
			rresult2 = CategoryService.updateCategoryFU(dbc, category);
			if (rresult1 == 1 && rresult2 == 1) {
				baseService.commitTransaction(dbc);
				rresult3 = 1;
			} else
				baseService.rollbackTransaction(dbc);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		return rresult3;
	}

	public static int updateDocument(Document document) {
		// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();
		return TempDocumentDAO.updateDocument(document);
	}

//	public static int updateSort(Document doc1, Document doc2) {
//		BaseService baseService = new BaseService();
//		DBConnect dbc = null;
//		// TempDocumentDAO documentDao = TempDocumentDAO.getInstance();
//		int result = 0;
//		// dbc = baseService.createTransaction(0);
//		// baseService.commitTransaction(dbc);
//		try {
//			int updatesort1 = 0;
//			int updatesort2 = 0;
//			dbc = baseService.createTransaction(0);
//			updatesort1 = TempDocumentDAO.updateSort(dbc, doc1);
//			updatesort2 = TempDocumentDAO.updateSort(dbc, doc2);
//			if (updatesort1 == 1 && updatesort2 == 1) {
//				baseService.commitTransaction(dbc);
//				result = 1;
//			} else
//				baseService.rollbackTransaction(dbc);
//		} catch (Exception e1) {
//			e1.printStackTrace();
//		}
//		return result;
//	}

	public static int move(boolean up, Document doc) {
		if (doc == null) {
			return -1;
		}
		BaseService baseService = new BaseService();
		DBConnect dbc = null;
		Document document = null;
		try {
		dbc = baseService.createTransaction(0);
		document = TempDocumentDAO.move(dbc, doc.getCid(), up, doc.getSort());
		if (document == null) {
			return -2;
		}
			int rs1 = TempDocumentDAO.updateSort(dbc, document.getDid(), doc
					.getSort());
			int rs2 = TempDocumentDAO.updateSort(dbc, doc.getDid(), document
					.getSort());
			if (rs1 == 0 && rs2 == 0) {

				baseService.commitTransaction(dbc);

				return 0;
			} else {
				baseService.rollbackTransaction(dbc);
				return -3;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -4;

	}
	
	public static int updateReadCount(int did){
		return TempDocumentDAO.updateReadCount(did);
	}
}
